import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'TransactionMonitoringM',
  state: {
    areaName: '江苏',
    getEchartsOne: { xaxis: [], series: undefined },
    getEchartsTwo: { xaxis: [], series: undefined },
    getEchartsThree1: { xaxis: [], series: undefined },
    getEchartsThree2: { xaxis: [], series: undefined },
    getEchartsFour: { xaxis: [], series: undefined },
    tableData: { dataSource: [], totalOrgan: null, totalProduct: null },
    accountData: { account: null, accountMoney: null },
    AreaData: [], // 选中区域的数据
  },

  effects: {
    // 获取产品规模
    *getEchartsOne({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getEchartsOne, payload);

      if (res && res.code == 0) {
        res.data &&
          res.data.series &&
          res.data.series.map((item) => {
            if (item.name == '数量') {
              item.yAxisIndex = 1;
            }
          });
        yield put({
          type: 'save',
          payload: {
            getEchartsOne: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },

    // 获取机构规模TOP5
    *getEchartsTwo({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getEchartsTwo, payload);
      if (res && res.code == 0) {
        let data;
        if (res.data?.series) {
          data = res.data;
        } else {
          data = { series: [], xaxis: [] };
        }

        yield put({
          type: 'save',
          payload: {
            getEchartsTwo: data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },

    // 获取交易笔数
    *getEchartsThree1({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getEchartsThree1, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            getEchartsThree1: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },
    // 获取交易金额
    *getEchartsThree2({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getEchartsThree2, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            getEchartsThree2: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },
    // 获取交易类型及其金额
    *getEchartsFour({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getEchartsFour, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            getEchartsFour: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },
    // 获取最新信息
    *getTableDate({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getTableDate, payload);
      if (res && res.code == 0) {
        if (res.data.dataSource !== null) {
          yield put({
            type: 'save',
            payload: {
              tableData: res.data,
            },
          });
        }
        callback(res.data);
      } else {
        message.error('获取数据失败');
      }
    },
    // 处理个区域数据
    *actionAreaData({ payload, callback }, { call, put }) {
      const data = payload.data;
      const areaName = payload.areaName;
      let areadata = [];
      if (data && data.length) {
        data.map((item) => {
          if (item.name == areaName) {
            areadata = item.datas;
          }
        });
        yield put({
          type: 'save',
          payload: {
            AreaData: areadata,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            AreaData: [],
          },
        });
      }
    },
    // 获取最新信息
    *getAccount({ payload, callback }, { call, put }) {
      const res = yield call(api.TransactionMonitoring.getAccount, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            accountData: res.data,
          },
        });
      } else {
        message.error('获取数据失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
